﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPixmap backTu(":/1.jpg");
    QBrush backColor(backTu);
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window, backColor);
    this->setPalette(palette);
    setWindowFlag(Qt::FramelessWindowHint);
    //setAttribute(Qt::WA_TranslucentBackground);
    QFile fp(":/style_1.qss");
    if(fp.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QTextStream out(&fp);
        QString style=out.readAll();
        fp.close();
        QApplication *app_1=static_cast<QApplication*>(QApplication::instance());
        app_1->setStyleSheet(style);
    }

}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QRect rect_1=ui->label->geometry();
    QRect rect_2=ui->label_2->geometry();
    if(rect_1.contains(event->pos()))
    {
        pos=event->pos();
        qDebug("鼠标点击");
        return;
    }
    if(rect_2.contains(event->pos()))
    {
        xpos=event->pos();
        qDebug("准备退出");
    }

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(pos==event->pos())
    {
        QString photoName=QFileDialog::getOpenFileName(
                    this,
                    "选择头像",
                    "",
                    "JPG(*.jpg);;PNG(*.png)"
                    );
        if(!photoName.isEmpty())
        {
            QPixmap photo_1(photoName);
            ui->label->setPixmap(photo_1);
            ui->label->setScaledContents(true);
            qDebug("头像切换成功");
        }
        else
        {
            qDebug("取消头像选择");
        }
        return;
    }
    if(xpos==event->pos())
    {
        qDebug("退出成功");
        close();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

